<script setup>
import Header from "./components/Header.vue";
import List from "./components/List.vue";
import Footer from "./components/Footer.vue";
import PubSub from "pubsub-js";
</script>

<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <Header @add="addItem" />
      <List :todos="todos" />
      <Footer
        :counter="counter"
        @remove-done="removeDoneItem"
        @done-all="doneAllItem"
      />
    </div>
  </div>
</template>

<script>
import { EVENT_CHECK_ITEM, MSG_DELETE_ITEM, EVENT_EDIT_ITEM } from "./consts";

const storeKey = "todoList";
export default {
  data() {
    return {
      todos: [],
      token: "",
    };
  },
  mounted() {
    const storedTodoList = window.localStorage.getItem(storeKey);
    if (storedTodoList) {
      this.todos = JSON.parse(storedTodoList);
    }

    this.$bus.$on(EVENT_CHECK_ITEM, (id) => {
      this.checkItem(id);
    });

    this.token = PubSub.subscribe(MSG_DELETE_ITEM, (_, id) => {
      this.deleteItem(id);
    });

    this.$bus.$on(EVENT_EDIT_ITEM, (todo) => {
      this.editItem(todo);
    });
  },
  beforeDestroy() {
    this.$bus.$off(EVENT_CHECK_ITEM);
    PubSub.unsubscribe(this.token.del);
    this.$bus.$off(EVENT_EDIT_ITEM);
  },
  watch: {
    todos: {
      deep: true,
      handler(value) {
        window.localStorage.setItem(storeKey, JSON.stringify(value));
      },
    },
  },
  computed: {
    counter() {
      const all = this.todos.length;
      // const done = this.todos.filter((t) => t.done).length;
      const done = this.todos.reduce(
        (count, t) => (t.done ? count + 1 : count),
        0
      );
      return {
        all,
        done,
      };
    },
  },
  methods: {
    addItem(item) {
      this.todos.unshift(item);
    },
    checkItem(id) {
      const idx = this.todos.findIndex((t) => t.id === id);
      this.todos[idx].done = !this.todos[idx].done;
    },
    deleteItem(id) {
      this.todos = this.todos.filter((t) => t.id !== id);
    },
    doneAllItem(done) {
      this.todos.map((t) => (t.done = done));
    },
    removeDoneItem() {
      this.todos = this.todos.filter((t) => !t.done);
    },
    editItem(todo) {
      const idx = this.todos.findIndex((t) => t.id === todo.id);
      this.todos = [
        ...this.todos.slice(0, idx),
        todo,
        ...this.todos.slice(idx + 1),
      ];
    },
  },
};
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}
.btn-primary {
  color: #fff;
  background-color: #2563eb;
  border: 1px solid #1d4ed8;
}

.btn-primary:hover {
  color: #fff;
  background-color: #1d4ed8;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
